<template>
    <div class="major-add">
        <el-form label-width="80px" ref="form" :model="form" :rules="rules">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="学号" prop="number">
                <el-input v-model="form.number" placeholder="请输入学号(12位)" />
            </el-form-item>
            <el-form-item label="所属班级" prop="classId">
                <el-select filterable v-model="form.classId" placeholder="请选择班级">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" placeholder="请输入密码(六位)" />
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
            <el-form-item label="生日" prop="birthday">
                <el-date-picker v-model="form.birthday" type="date" placeholder="选择生日">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-radio v-model="form.sex" label="1">男</el-radio>
                <el-radio v-model="form.sex" label="0">女</el-radio>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer" style="display: flex; justify-content: flex-end;">
            <el-button type="primary" @click="onsubmit">确定</el-button>
            <el-button type="primary" @click="handclose" plain>取消</el-button>
        </span>
    </div>
</template>
<script>
import { studentup, adminstudentid, classnames } from '@/api/student';
export default {
    props: {
        id: { type: Number, default: null }
    },
    data() {
        return {
            form: { name: '', sex: "1", classId: '', number: '', password: '', email: '', birthday: '' },
            options: [{
                value: '',
                label: ''
            }],
            rules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ],
                classId: [
                    { required: true, message: '请选择班级', trigger: 'change' }
                ],
                number: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' }
                ],
            },
        }
    },
    mounted() {
        classnames().then(res => {
            console.log(res);
            this.options = (res.data || []).map(item => ({
                label: item.name,
                value: item.id
            }));
        })
        if (this.id) {
            adminstudentid(this.id).then(res => {
                if (res.code === 0) {
                    this.form = res.data;
                    this.form.sex=String(this.form.sex);
                } else {
                    this.$message.error('获取专业信息失败');
                }
            });
        }
    },
    methods: {
        async onsubmit() {
            this.$refs["form"].validate(async flag => {
                if (flag) {
                    console.log(this.form)
                    const res = await studentup(this.form);
                    if (res.code == 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                        });
                        this.$emit('done');
                    } else {
                        console.log(res);
                        this.$message({
                            message: '操作失败',
                            type: 'error',
                        });
                    }
                }
            })
        },
        handclose() {
            this.$emit('close')
        },
    }
}
</script>
